package com.haosuwei.mybatis_test.dao.impl;

import com.haosuwei.mybatis_test.dao.Book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BooksDaoImpl {

    private SqlSession sqlSession = null;

    @Before
    public void init() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Test
    public void selectBooks() {
        List<Map<String, Object>> bookList = this.sqlSession.selectList(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.selectBooks");
        bookList.forEach(temp -> System.out.println(temp));
    }

    @Test
    public void select1() {
        Map<String, Object> bookMap = this.sqlSession.selectOne(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.select1");
        bookMap.forEach((k,v) -> System.out.println(k+","+v));
    }

    @Test
    public void select2() {
        Map<String, Object> bookMap = this.sqlSession.selectOne(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.select2", 3L);
        bookMap.forEach((k,v) -> System.out.println(k+","+v));
    }

    @Test
    public void select3() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("price", 2);
        paramMap.put("num", 100);
        List<Map<String, Object>> bookList = this.sqlSession.selectList(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.select3", paramMap);
        bookList.forEach(temp -> System.out.println(temp));
    }

    @Test
    public void select4() {
        Book book = new Book();
        book.setPrice(2.0f);
        book.setNum(100);
        List<Map<String, Object>> bookList = this.sqlSession.selectList(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.select4", book);
        bookList.forEach(temp -> System.out.println(temp));
    }

    @Test
    public void select5() {
        Book book = new Book();
        book.setPrice(2.0f);
        book.setNum(100);
        List<Book> bookList = this.sqlSession.selectList(
                "com.haosuwei.mybatis_test.dao.impl.BooksDaoImpl.select5", book);
        bookList.forEach(temp -> System.out.println(temp));
    }

    @Test
    public void insertOne() {
        Book book = new Book();
        book.setId(0L);
        book.setPrice(2.0f);
        book.setName("xxx");
        book.setNum(250);
        book.setDesc("xxxx");
        int flag = this.sqlSession.insert("com.haosuwei.mybatis_test.dao.impl" +
                ".BooksDaoImpl.insertOne",book);
        this.sqlSession.commit();
    }

}
